library(sf)
library(fs)
library(tidyverse)
library(ppmf)
library(redist)
library(geomander)
library(here)

sf::sf_use_s2(FALSE)

source("R/00_custom_functions.R")

# Load data -------
# add NC data (see README for documentaion)
nc_shp <- st_make_valid(readRDS('data/NC/nc_shp_initial.rds'))

# NC PPMF
ppmf19 <- read_ppmf("NC", "data-raw/ppmf_20210608_P.csv") %>%
    add_geoid() %>% agg() %>% breakdown_geoid() %>%
    rename_with(~ add_pref(.x,pref = 'v19'), starts_with(c('pop', 'vap')))

ppmf12 <- read_ppmf("NC", "data-raw/ppmf_12.csv") %>%
    add_geoid() %>% agg() %>% breakdown_geoid() %>%
    rename_with(~ add_pref(.x,pref = 'v12'), starts_with(c('pop', 'vap')))

ppmf4 <- read_ppmf("NC", "data-raw/ppmf_04.csv") %>%
    add_geoid() %>% agg() %>% breakdown_geoid() %>%
    rename_with(~ add_pref(.x,pref = 'v4'), starts_with(c('pop', 'vap')))

# Census 2010
blk_census <- create_block_table(state = 'NC')


# Join PPMF and Census ----
blk_shp_NC <- blk_census %>%
    left_join(ppmf19, by = 'GEOID') %>%
    left_join(ppmf12, by = 'GEOID') %>%
    left_join(ppmf4, by = 'GEOID')

# Cleanup
# and remove duplicates
# and set missing block pop/vap to 0
# add block_group back (dropped by contains('.'))
blk_shp_NC <- blk_shp_NC %>% select(-contains('.'))
blk_shp_NC[is.na(blk_shp_NC)] <- 0
blk_shp_NC <- blk_shp_NC %>% breakdown_geoid()

blk_shp <- st_make_valid(blk_shp_NC)


# Geomatch ------
nc_matchb2p <- geo_match(blk_shp, nc_shp, method = 'area')
blk_prec <- blk_shp %>% mutate(precinct = nc_matchb2p)


# aggregate to precinct from blocks ----
ppmf_at_prec <- ppmf_block2prec(blk_prec, precinct)

# join them together ----
nc_all <- nc_shp %>%
    mutate(precinct = row_number()) %>%
    left_join(ppmf_at_prec, by = 'precinct')

# Save -----
# Save for nc_partisan.R
write_rds(nc_all, "data/NC/nc_shp.rds", compress = "xz")

# Intermediate -- don't track
write_rds(blk_prec, file = "data/NC/prepped_nc_block.Rds", compress = "xz")
